using Microsoft.Extensions.DependencyInjection;
using Volo.Abp;
using Volo.Abp.Authorization;
using Volo.Abp.BackgroundJobs;
using Volo.Abp.Domain;
using Volo.Abp.Domain.Entities.Events;
using Volo.Abp.Guids;
using Volo.Abp.Modularity;
using Volo.Abp.MultiTenancy;
using Volo.Abp.PermissionManagement;
using Volo.Abp.Uow;
using Volo.Abp.Identity;
using UserManagementService.Users;
using UserManagementService.Organizations;

namespace UserManagementService;

[DependsOn(
    typeof(AbpAuthorizationModule),
    typeof(AbpBackgroundJobsModule),
    typeof(AbpDomainModule),
    typeof(AbpIdentityDomainModule),
    typeof(UserManagementServiceDomainModule),
    typeof(AbpTestBaseModule)
)]
public class UserManagementServiceDomainTestModule : AbpModule
{
    public override void ConfigureServices(ServiceConfigurationContext context)
    {
        // 配置测试用的GUID生成器
        context.Services.AddSingleton<IGuidGenerator, SequentialGuidGenerator>();

        // 配置多租户
        Configure<AbpMultiTenancyOptions>(options =>
        {
            options.IsEnabled = true;
        });

        // 配置工作单元
        Configure<AbpUnitOfWorkOptions>(options =>
        {
            options.TransactionBehavior = UnitOfWorkTransactionBehavior.Disabled;
        });

        // 配置权限管理
        Configure<PermissionManagementOptions>(options =>
        {
            options.IsDynamicPermissionStoreEnabled = false;
        });

        // 注册测试用的仓储
        context.Services.AddTransient<IExtendedUserRepository, FakeExtendedUserRepository>();
        context.Services.AddTransient<IOrganizationRepository, FakeOrganizationRepository>();

        // 注册测试用的服务
        context.Services.AddTransient<IUserPermissionService, FakeUserPermissionService>();
        context.Services.AddTransient<IOrganizationManagementService, FakeOrganizationManagementService>();

        // 配置事件总线
        Configure<AbpDistributedEventBusOptions>(options =>
        {
            options.Outboxes.Configure(config =>
            {
                config.IsUsed = false;
                config.ExecutionSchedulerPeriod = 0;
            });
        });
    }
}